﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace EbbinghausWord.Entities
{
    /// <summary>
    /// 词典单词
    /// </summary>
    public class DictionaryWord
    {
        /// <summary>
        /// 英语
        /// </summary>
        public string English { get; set; }

        /// <summary>
        /// 音标
        /// </summary>
        public string Soundmark { get; set; }

        /// <summary>
        /// 中文
        /// </summary>
        public string Chinese { get; set; }

        /// <summary>
        /// 词组
        /// </summary>
        public List<WordGroupEntity> WordGroups { get; set; }

        /// <summary>
        /// 例句
        /// </summary>
        public List<SentenceEntity> Sentences { get; set; }

        /// <summary>
        /// 同义词
        /// </summary>
        public List<SynonymEntity> Synonyms { get; set; }


        /// <summary>
        /// 从数据实体中获得实体对象
        /// </summary>
        /// <returns></returns>
        public static DictionaryWord FromTable(XElement element)
        {
            var english = element.Element("E").Value;
            var soundmark = element.Element("SM").Value;
            var wordChinese = element.Element("C").Value;

            //词组
            var wordGroups = new List<WordGroupEntity>();
            var wordGroupElements = element.Element("WGS").Elements("WG");
            foreach (XElement xElement in wordGroupElements)
            {
                wordGroups.Add(new WordGroupEntity()
                {
                    English = xElement.Element("E").Value,
                    Chinese = xElement.Element("C").Value,
                });
            }

            //例句
            var sentences = new List<SentenceEntity>();
            var sentenceElements = element.Element("Sents").Elements("Sent");
            foreach (XElement xElement in sentenceElements)
            {
                sentences.Add(new SentenceEntity()
                {
                    English = xElement.Element("E").Value,
                    Chinese = xElement.Element("C").Value,
                });
            }

            //同义词
            var synonyms = new List<SynonymEntity>();
            var synonymsElements = element.Element("Syns").Elements("Syn");
            foreach (XElement xElement in synonymsElements)
            {
                synonyms.Add(new SynonymEntity()
                {
                    English = xElement.Element("E").Value,
                    Chinese = xElement.Element("C").Value,
                });
            }

            return new DictionaryWord()
            {
                English = english,
                Soundmark = soundmark,
                Chinese = wordChinese,

                WordGroups = wordGroups,
                Sentences = sentences,
                Synonyms = synonyms,
            };
        }
    }
}
